import http from '../../../http/http'
import * as url from '../../../api/cfApi'
import {Message} from 'element-ui';
//餐桌页面数据存放
const state = {
  classtyList: [],
  dishesList: [],
  showPage: 1,
}
const mutations = {
  setClassfy(state,list=[]) {
    let classlist = list.map(item => item.dishesClassify.name)
    state.classtyList = [...new Set(classlist)]
  },
  setDishes(state,list=[]){
    state.dishesList = list
    // if (state.classtyList.length===0) {
    //   this.setClassfy(list)
    // }
  },
}
const actions = {
  dishesSelectAC(context,obj){
    return http({
      url: '/api'+url.DISHES_SEARCH,
      method: 'post',
      data: obj
    }).then(res =>{
      if (res.data.code === '200') {
        context.state.classtyList.length===0 ? context.commit('setClassfy',res.data.dishesList) : ''
        context.commit('setDishes',res.data.dishesList)   //设置显示的数据
        return res.data.dishesList
      }
    }).catch(err => {
      console.log(err)
    })
    
  },
  
}

const  getters= {
}
export default {
  namespaced: true,//此属性将getters,actions,mutations局部化
  state,
  actions,
  mutations,
  getters
}
